
//
//  NewsScrollView.m
//  HenanOA
//
//  Created by 党阳阳 on 14-5-21.
//  Copyright (c) 2014年 broadwave.com. All rights reserved.
//

#import "NewsScrollView.h"

#import "NewsScrollViewCell.h"

@interface NewsScrollView()
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (assign) BOOL didEndAnimate;
@property (assign) float previousTouchPoint;


@end

@implementation NewsScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(self.frame.size.width*3, self.frame.size.height);
        _scrollView.autoresizesSubviews = YES;
        for (int i=0; i<3; i++) {
            
            UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(i*_scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
            view.image = [UIImage imageNamed:@"base_detail_default_pic.png"];
            [_scrollView addSubview:view];
        }
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(260, self.frame.size.height-20, 60, 20)];
        _pageControl.numberOfPages = 3;
        _pageControl.currentPage = 0;
        _pageControl.userInteractionEnabled = NO;
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        
        [self addSubview:_scrollView];
        [self addSubview:_pageControl];

    
    }
    return self;
}

- (void)setArrayOfImages:(NSMutableArray *)arrayOfImages
{
    for (UIView *view in [_scrollView subviews]) {
        [view removeFromSuperview];
    }
    
    _scrollView.contentSize = CGSizeMake(self.frame.size.width*[arrayOfImages count], self.frame.size.height);
    _scrollView.autoresizesSubviews = YES;
    for (UIView *view in [_scrollView subviews]) {
        [view removeFromSuperview];
    }
    
    for (int i=0; i<3; i++) {
        
        CGRect rect = CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"URL" ofType:@"plist"];
        NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        
        NSString *imageString = [NSString stringWithFormat:@"http://%@%@",[data objectForKey:@"IPAndPort"],[[arrayOfImages objectAtIndex:i] objectForKey:@"image"]];
       
        NSString *titleString = [[[arrayOfImages objectAtIndex:i] objectForKey:@"title"]stringByReplacingOccurrencesOfString:@"&mdash;" withString:@"--"];
        
        NewsScrollViewCell *cell = [[NewsScrollViewCell alloc]initWithFrame:rect imageString:imageString titleString:titleString];
    
        
         cell.tag= [[[arrayOfImages objectAtIndex:i] objectForKey:@"id"] intValue]*10+i;
        
    
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        cell.userInteractionEnabled = YES;
        [cell addGestureRecognizer:tapGesture];
        
        
        [_scrollView addSubview:cell];
    }
    
    _pageControl.numberOfPages = arrayOfImages.count;
    
    
    
}

#pragma -mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_scrollView]) {
        
        NSInteger page = fabs(scrollView.contentOffset.x/scrollView.frame.size.width);
        _pageControl.currentPage = page;
   
    }
}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    
}


- (void)tap:(id)sender
{
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;

   
    NSString *newId = [NSString stringWithFormat:@"%ld",(long)tap.view.tag/10];
    [_delegate selectNewsScrollViewCellForId:newId];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
